<?php
// No direct access to this file
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

class ManagerViewProduct extends JView {
	
	var $table = null;
	var $arr_serial = array();
	var $arr_pond = array();
	var $arr_customer = array();
	var $arr_fishclass = array();
	var $arr_status = array();
	var $arr_type = array();
	
    function __construct() {
        $document = JFactory::getDocument();
        $document->addScript('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
        $document->addScript('/administrator/templates/bluestork/js/pim.js');

        ManagerHelper::showMenuPim("product");
        $this->table =& JTable::getInstance('Product', 'Table');	         
		
		$Customer = JModel::getInstance('Customer', 'PimModel');
		$this->arr_customer = $Customer->getListSelectBox();
		$this->arr_type = $Customer->getListType();
		
		$FishClass = JModel::getInstance('FishClass', 'PimModel');
		$this->arr_fishclass = $FishClass->getListSelectBox();
        
		$Pond = JModel::getInstance('Pond', 'PimModel');
		$this->arr_pond = $Pond->getListSelectBox();
        
        $pondCode = ''; // get serial belong to pond
        if (!empty($this->arr_pond)) {
            $pondCode = $this->arr_pond[0]->code;
        }        
        $Serial = JModel::getInstance('Serial', 'PimModel');
		$this->arr_serial = $Serial->getListSelectBox($pondCode, ManagerHelper::SERIAL_BELONG_POND);
		
		$Product = JModel::getInstance('Product', 'PimModel');
		$this->arr_status = $Product->getListStatus();		
		
        $lists['serial_code'] = JHTML::_('select.genericlist', $this->arr_serial, "serial_code", "class='inputbox'", 'code', 'code');
        $lists['pond_code'] = JHTML::_('select.genericlist', $this->arr_pond, "pond_code", "class='inputbox' onchange='loadSerial(this.value);'", 'code', 'name');
        $lists['class_code'] = JHTML::_('select.genericlist', $this->arr_fishclass, "class_code", "class='inputbox'", 'code', 'name');
        $lists['customer_code'] = JHTML::_('select.genericlist', $this->arr_customer, "customer_code", "class='inputbox'", 'code', 'name');
        $lists['status'] = JHTML::_('select.genericlist', $this->arr_status, "status", "class='inputbox'",'id', 'name');
        
		$this->assign('lists', $lists);
		$this->assign('arr_pond', $this->arr_pond);
		$this->assign('arr_customer', $this->arr_customer);
		$this->assign('arr_fish_class', $this->arr_fishclass);
		$this->assign('arr_status', $this->arr_status);
		$this->assign('arr_type', $this->arr_type);
		
        parent::__construct();
    }	
    
	function display() {
		$mainframe = JFactory::getApplication();
        $this->setLayout('display');
        
        $post = JRequest::get('post', JREQUEST_ALLOWHTML);
        if (isset($post['add_button'])) {
        	$this->table->saveRecord($post);
        }
        
        $where = $this->filter($mainframe);
        
        ManagerHelper::showToolBar('Product Management', true);
        						
        $limit				= $mainframe->getUserStateFromRequest( 'products.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
        $limitstart 		= $mainframe->getUserStateFromRequest( 'products.limitstart', 'limitstart', 0, 'int' );
        $filter_order		= $mainframe->getUserStateFromRequest( 'products.filter_order', 'filter_order', 'id', 'cmd' );
        $filter_order_Dir	= $mainframe->getUserStateFromRequest( 'products.filter_order_Dir', 'filter_order_Dir', ManagerHelper::ORDER_DIR, 'word' );

        $Model = JModel::getInstance('Product', 'PimModel');
        $Model->getData($total, $dbrow, $filter_order, $filter_order_Dir, $limitstart, $limit, $where);
		
        jimport('joomla.html.pagination');
        $pageNav = new JPagination( $total, $limitstart, $limit );

        $this->assign('TOTAL_ROW', $total);
        $this->assign('dbrow', $dbrow);
        $this->assign('LIST_FOOTER_PAV', $pageNav->getListFooter());
        $this->assign('FORM_ORDER', $filter_order);
        $this->assign('FORM_ORDER_DIR', $filter_order_Dir);
      
        parent::display();
    }
    
 	function addForm() {		
		$mainframe = JFactory::getApplication();
		$this->setLayout('form');					
		$cid = JRequest::getVar('cid');
	    $rows = $this->table;
	    
        if(!empty($cid)) {
			$rows->load($cid[0]);
            
            $Serial = JModel::getInstance('Serial', 'PimModel');
			$arr_serial = $Serial->getListSelectBox($rows->pond_code, ManagerHelper::SERIAL_BELONG_POND); // get serial belong to pond
            
			$lists['serial_code'] = JHTML::_('select.genericlist', $arr_serial, "serial_code", "class='inputbox'", 'code', 'code', $rows->serial_code);
	        $lists['pond_code'] = JHTML::_('select.genericlist', $this->arr_pond, "pond_code", "class='inputbox' onchange='loadSerial(this.value);'", 'code', 'name', $rows->pond_code);
	        $lists['class_code'] = JHTML::_('select.genericlist', $this->arr_fishclass, "class_code", "class='inputbox'", 'code', 'name', $rows->class_code);
	        $lists['customer_code'] = JHTML::_('select.genericlist', $this->arr_customer, "customer_code", "class='inputbox'", 'code', 'name', $rows->customer_code);
	        $lists['status'] = JHTML::_('select.genericlist', $this->arr_status, "status", "class='inputbox'",'id', 'name', intval($rows->status));
	        
			$this->assign('lists', $lists);
			$this->assign('rows', $rows);
		}		
		
		parent::display();
    }
    
	function addFormPopup() {		
		$this->setLayout('form_popup');					
		parent::display();
    }
    
    function filter($mainframe) {
    	$where = array();
 		$filter_serial = $mainframe->getUserStateFromRequest('filter.products.serial', 'filter_serial', '', 'cmd');
 		$filter_pond = $mainframe->getUserStateFromRequest('filter.products.pond', 'filter_pond', '', 'int');
 		$filter_fishclass = $mainframe->getUserStateFromRequest('filter.products.fishclass', 'filter_fishclass', '', 'int');
 		$filter_customer = $mainframe->getUserStateFromRequest('filter.products.customer', 'filter_customer', '', 'int');
 		$filter_status = $mainframe->getUserStateFromRequest('filter.products.status', 'filter_status', '', 'int');
 		
    	if ($filter_serial) {
 			$where[] = 'se.code LIKE "%' . $filter_serial . '%"';
 		}
    	if ($filter_pond) {
 			$where[] = "p.pond_code=$filter_pond";
 		}
    	if ($filter_fishclass) {
 			$where[] = "p.class_code=$filter_fishclass";
 		}
    	if ($filter_customer) {
 			$where[] = "p.customer_code=$filter_customer";
 		}
 		if ($filter_status !== '' && $filter_status != -1) {
 			$where[] = "p.status=$filter_status";
 		}
 		
 		$mainframe->setUserState('filter_products', $where);
 		
 		$this->assign('filter_serial', $filter_serial);
 		$this->assign('filter_pond', $filter_pond);
 		$this->assign('filter_fishclass', $filter_fishclass);
 		$this->assign('filter_customer', $filter_customer);
 		$this->assign('filter_status', $filter_status);
 		
 		return $where;
    }
}
